package com.newbingo.action;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.googlecode.jsonplugin.annotations.JSON;
import com.newbingo.dto.PeriodHistoryDto;
import com.newbingo.entity.Game;
import com.newbingo.entity.GameTable;
import com.newbingo.entity.Lottery;
import com.newbingo.entity.PeriodGame;
import com.newbingo.service.imple.IGameService;
import com.newbingo.service.imple.IGameTableService;
import com.newbingo.service.imple.IPeriodGameService;
import com.newbingo.util.Pager;
import com.newbingo.util.GameTree;

public class PeriodGameAction extends ActionSupport {
	private IGameService gameService = null;
	private IPeriodGameService periodService = null;
	private IGameTableService gameTableService;

	private PeriodGame periodGame = null;
	private GameTable gameTable = null;

	/* 分页条件 */
	private int start;
	private int limit=10;
	/* 查询条件 */
	private String type;
	private String date;
	private Pager pager;

	/**
	 * Integer gtno; 游戏桌Id String periodTime; 开奖时间 String periodNum; 游戏期号 String
	 * gamename; 游戏名称 String tablename; 游戏桌名称 String lotteryNum=""; 开奖号码
	 */

	// 页面根据json输出游戏和游戏桌的树型菜单,点击获取最新一期的游戏
	private String gameJson;
	private int gtno;

	// 保存本期游戏开奖结果
	private int pgno;
	private int[] lotteries = new int[20];
	//页面返回情况
	private Boolean success = false;
	//计算出离开奖还有多长时间，并发送到客户端由客户端来验证
	private int drawLotteryTime;

	/** ******** 操作方法 *********** */
	/**
	 * 获取所有游戏的信息，调用Tree的方法转换为JSON赋给gameJson
	 */
	public String getGameToJson() {
		// 查询所有数据进行JSON转换
		gameJson = GameTree.getGamesJson(gameService.findAll());
		return "gametree";
	}

	/**
	 * 根据游戏桌号查询出最新一期的游戏，输出到页面，由操作员输入中奖号码
	 * 
	 * @return
	 */
	public String findGameTable() {
		periodGame = periodService.getCurrentPeriodGame(gtno);
		if(periodGame==null)
			success=false;
		else{
			//获取服务器时间
			Date now=new Date();
			if(periodGame.getPeriodTime()!=null)
				drawLotteryTime=(int)((periodGame.getPeriodTime().getTime()-now.getTime())/1000);
			if(drawLotteryTime<0)
				drawLotteryTime=0;
			success = true;
		}
		return SUCCESS;
	}

	/**
	 * 保存本期游戏开奖
	 * 
	 * @return
	 */
	public String savePeriodGameResult() {
		periodService.savePeriodGameResult(gtno, lotteries);
		success = true;
		return SUCCESS;
	}

//	/**
//	 * 增加
//	 */
//	public String create() {
//		periodService.save(periodGame);
//		this.setPeriodGame(null);
//		success = true;
//		return SUCCESS;
//	}
//
//	/**
//	 * 删除
//	 */
//	public String delete() {
//		periodService.delete(periodGame);
//		success = true;
//		return SUCCESS;
//	}
//
//	/**
//	 * 修改
//	 */
//	public String merge() {
//		if (periodGame == null) {
//			success = false;
//		} else {
//			periodService.merge(periodGame);
//			success = true;
//		}
//		return SUCCESS;
//	}

	/**
	 * 分页查询
	 */
	public String findByPage() {
		this.setPager(periodService.findByPage(start, limit, type));
		success = true;
		return SUCCESS;
	}

	/**
	 * 根据gtno查询
	 */
	public String findByGtNO() {
		this.setPager(periodService.findByGtNO(start, limit, gameTable
				.getGtno()));
		success = true;
		return SUCCESS;
	}

	/**
	 * 根据编号查询
	 */
	public String findById() {
		this.setPeriodGame(periodService.findById(periodGame.getPgno()));
		success = true;
		return SUCCESS;
	}

	/**
	 * 查询游戏历史开奖记录
	 * 
	 * @param gtno
	 *            游戏桌编号
	 * @param date
	 *            时间
	 * @return Pager
	 */
	public String findHistoryRecord() {
		this.setPager(periodService.findHistoryRecord(start, limit, gameTable
				.getGtno(), this.getDate()));
		success = true;
		return SUCCESS;

	}

	public String getGameJson() {
		return gameJson;
	}

	public void setGameJson(String gameJson) {
		this.gameJson = gameJson;
	}
	
	@JSON(serialize = false)
	public GameTable getGameTable() {
		return gameTable;
	}

	public void setGameTable(GameTable gameTable) {
		this.gameTable = gameTable;
	}

	@JSON(serialize = false)
	public IGameTableService getGameTableService() {
		return gameTableService;
	}

	public void setGameTableService(IGameTableService gameTableService) {
		this.gameTableService = gameTableService;
	}


	@JSON(serialize = false)
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}

	public int getGtno() {
		return gtno;
	}

	public void setGtno(int gtno) {
		this.gtno = gtno;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	@JSON(serialize = false)
	public IPeriodGameService getPeriodService() {
		return periodService;
	}

	public void setPeriodService(IPeriodGameService periodService) {
		this.periodService = periodService;
	}

	public PeriodGame getPeriodGame() {
		return periodGame;
	}

	public void setPeriodGame(PeriodGame periodGame) {
		this.periodGame = periodGame;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	@JSON(serialize = false)
	public IGameService getGameService() {
		return gameService;
	}

	public void setGameService(IGameService gameService) {
		this.gameService = gameService;
	}

	public int getPgno() {
		return pgno;
	}

	public void setPgno(int pgno) {
		this.pgno = pgno;
	}

	public int[] getLotteries() {
		return lotteries;
	}

	public void setLotteries(int[] lotteries) {
		this.lotteries = lotteries;
	}
	
	public int getDrawLotteryTime() {
		return drawLotteryTime;
	}

	public void setDrawLotteryTime(int drawLotteryTime) {
		this.drawLotteryTime = drawLotteryTime;
	}

}
